package MainBoard;

import Dialog.ChooseRoleDialog;
import Dialog.EndingDialog;
import Dialog.LocalHostIPDialog;
import Menu.InitialMenu;
import Model.Chess;
import Model.ChessCoord;
import Model.VictoryJudgement;
import Net.PlayChess;
import Net.Server;

import java.awt.event.MouseEvent;
import java.io.IOException;

public class PVPChessBoard extends ChessBoard {
    public static int YOUR_TURN = 1;
    public static int OPP_TURN = 2;
    public static int WAIT = 0;
    //    游戏主界面
    private final PVPMainBoard mainBoard;
    //    用户选择的棋色
    private int role;
    private int opponentRole;
    //    设置游戏状态
    private int result;

    /**
     * 人人对弈棋盘的构造函数
     *
     * @param mainBoard 人人对弈主界面
     */
    public PVPChessBoard(PVPMainBoard mainBoard) {
        this.mainBoard = mainBoard;
        this.initStep();
        this.endingDialog = new EndingDialog(mainBoard, "游戏结束", true);
    }

    /**
     * 以对话框设置用户执棋的颜色
     *
     * <p>会通过弹出对话框来使用户选择执棋颜色
     * 该方法将在MainBoard中被调用</p>
     */
    public void setRoleDialog() {
        ChooseRoleDialog chooseRoleDialog = new ChooseRoleDialog(mainBoard, "执棋选择", true);
        chooseRoleDialog.setVisible(true);
        this.role = chooseRoleDialog.getMessage();
        this.opponentRole = this.role ^ 3;
    }

    /**
     * 设置用户执棋颜色
     *
     * @param role 用户执棋颜色
     */
    public void setRole(int role) {
        this.role = role;
    }

    /**
     * 获取用户执棋颜色
     *
     * @return 用户执棋颜色
     */
    public int getRole() {
        return role;
    }

    /**
     * 设置对方执棋颜色
     *
     * @param opponentRole 对方执棋颜色
     */
    public void setOpponentRole(int opponentRole) {
        this.opponentRole = opponentRole;
    }

    /**
     * 获取对方执棋颜色
     *
     * @return 对方执棋颜色
     */
    public int getOpponentRole() {
        return opponentRole;
    }

    /**
     * 用户悔棋
     */
    public void backStep() {
        if (stepCount >= 2) {
            chessCoord[step[stepCount][0]][step[stepCount][1]] = 0;
            chessCoord[step[stepCount - 1][0]][step[stepCount - 1][1]] = 0;
            stepCount = stepCount - 2;
        }
    }

    /**
     * 设置游戏当前状态
     *
     * @param result 游戏状态，1表示正在游戏，0表示游戏结束
     */
    public void setResult(int result) {
        this.result = result;
    }

    /**
     * 获取游戏当前游戏状态
     *
     * @return 当前游戏状态
     */
    public int getResult() {
        return result;
    }

    /**
     * 游戏结束事件判定
     *
     * @param winner 游戏的获胜者
     */
    public void overEvent(int winner) {
        if (winner == role) {
            result = GAME_OVER;
            initChessBoard();
            endingDialog.setWinnerInfo("恭喜获得胜利！");
            endingDialog.setVisible(true);
            setClickable(PVEMainBoard.CAN_NOT_CLICK);

            if (endingDialog.getMessage() == EndingDialog.back) {
                mainBoard.dispose();
                if (mainBoard.getNetContainer() instanceof Server) {
                    mainBoard.getServerThread().interrupt();
                } else {
                    mainBoard.getClientThread().interrupt();
                }
                new InitialMenu();
            }
        } else if (winner == opponentRole) {
            result = GAME_OVER;
            initChessBoard();
            endingDialog.setWinnerInfo("很抱歉您失败了。");
            endingDialog.setVisible(true);
            setClickable(PVEMainBoard.CAN_NOT_CLICK);

            if (endingDialog.getMessage() == EndingDialog.back) {
                mainBoard.dispose();
                if (mainBoard.getNetContainer() instanceof Server) {
                    mainBoard.getServerThread().interrupt();
                } else {
                    mainBoard.getClientThread().interrupt();
                }
                new InitialMenu();
            }
        }
    }

    /**
     * 游戏结束后重新初始化棋盘
     */
    private void initChessBoard() {
        try {
            mainBoard.getTimeThread().interrupt();
        } catch (Exception e) {
            e.printStackTrace();
        }
        initChessCoord();
        initStep();
        mainBoard.getProcessLabel().setText("等待双方准备");
        mainBoard.buttonStart.setText("开始游戏");
        mainBoard.buttonBack.setEnabled(false);
        mainBoard.buttonStart.setEnabled(true);
    }

    /**
     * 信息异常处理
     *
     * @param e 信息导致的异常
     */
    public void errorHandel(Exception e) {
        e.printStackTrace();
        mainBoard.getLocalHostIPDialog().getLocalHostLabel().setText("游戏异常");
        mainBoard.getLocalHostIPDialog().setVisible(true);
        if (mainBoard.getLocalHostIPDialog().getMessage() == LocalHostIPDialog.confirm) {
            initChessBoard();
        }
    }

    /**
     * 设置下棋的点击事件
     *
     * @param e 鼠标点击事件
     */
    @Override
    public void mousePressed(MouseEvent e) {
        int winner;
        if (clickable) {
            chessX = e.getX();
            chessY = e.getY();
            if (chessX >= 30 && chessX <= 570 && chessY >= 30 && chessY <= 570) {
                int x = (chessX - 20) / 30;
                int y = (chessY - 20) / 30;
                //    用户落棋判定
                if (chessCoord[x][y] == Chess.noChess) {
                    chessCoord[x][y] = role;
                    saveStep(x, y);
                    try {
                        mainBoard.getNetContainer().getOut().writeUTF("POS " + x + " " + y);
                    } catch (IOException ioException) {
                        errorHandel(ioException);
                    }
                    winner = VictoryJudgement.PPJudge(x, y, chessCoord, role);
                    overEvent(winner);
                    if (result == GAME_START) {
                        setClickable(false);
                        new Thread(new PlayChess(this.mainBoard, this.mainBoard.getNetContainer())).start();
                        mainBoard.getProcessLabel().setText("等待对方落棋");
                    }
                }
            }
        }
    }
}
